% % % %     I = imread(str1);        
% % % %     I = I*150;
% % % % %     imshow(I);
% % % % 
% % % %     se = strel('disk',1)
% % % % 
% % % %     background = imopen(I,se);
% % % % %     imshow(background)
% % % % 
% % % % 
% % % %     I2 = I - background;
% % % %     imshow(I2)

currentFolder = pwd;
for i = 1:length(currentFolder)-length('functions')
    act_Folder(i) = currentFolder(i);
end
str_append = '5mm\6-214'; 
str=strcat(act_Folder,'冷态piv\',str_append,'\'); 
str_read=strcat(str,'1_');
str_write=strcat(str,'newtest\');
mkdir(str_write);

%do we need a pre definition of avrg use 10 images? try

for ii=1:200 %this gap must no change!!   
    if ii<10
         str1 = strcat (str_read,'000',int2str(ii) , '.tif'); % 连接字符串形成图像的文件名
    elseif ii<100
         str1 = strcat (str_read,'00',int2str(ii) , '.tif');
    elseif ii<1000
         str1 = strcat (str_read,'0',int2str(ii) , '.tif');
    else
         str1 = strcat (str_read,int2str(ii) , '.tif');
    end
    
    
    %读取原图像
    Original_image=imread(str1);
    Original_image=256*Original_image;
    [len,wid]=size(Original_image);
    
    %去除异常亮度??good or not
    imshow(Original_image)
    standard = 20000; %Original_image(len/2,wid/2);
%     for i=1:len
%         for j=1:wid 
%             if Original_image(i,j)>(1.5*standard)
%                 Original_image(i,j) = Original_image(i,j)/1.5;
%             end
%         end
%     end
%     imshow(Original_image)

    avrge_len = ceil(len/50);  
    avrge_wid = ceil(wid/50);% 50 is too much or not ,test some result of 20 50 100  
    avrgFilter = fspecial('average',[avrge_len avrge_wid]);%average / gaussian 5
    J=imfilter(Original_image,avrgFilter,'replicate');
    imshow(J)
    
    avrg = double(standard) ./ double(J);
    Balanced_image = double(Original_image) .* avrg;
    Balanced_image = uint16(Balanced_image);
    imshow(Balanced_image);
    
    if ii<10
         str2 = strcat ('000',int2str(ii) , '.tif'); % 连接字符串形成图像的文件名。
    elseif ii<100
         str2 = strcat ('00',int2str(ii) , '.tif');
    elseif ii<1000
         str2 = strcat ('0',int2str(ii) , '.tif');
    else
         str2 = strcat ( int2str(ii) , '.tif');
    end       

   imwrite(Balanced_image,[str_write str2]);    


end


% 
% imhist(filter_image1)
% J = histeq(filter_image1);
% imhist(J)
% imshow(J)
% 
% imhist(filter_image2)
% J = histeq(filter_image2);
% imhist(J)
% imshow(J)
% 
% imhist(im2uint8(Original_image))
% J = histeq(im2uint8(Original_image));
% imhist(J)
% imshow(J)



% % str = 'F:\wanghui\冷态piv\';
% % str_append='2500_10mm\2';
% 
% row=1024;col=1024;
% % a=-330;b=510;r1=692;r2=1016;a_inner = a;a_outer = a;b_inner = b;b_outer = b;% 10mm
% %  a=-300;b=510;r1=692;r2=1016;a_inner = a;a_outer = a;b_inner = b;b_outer = b;% 20mm
%  a=-300;b=510;r1=692;r2=1016;a_inner = a;a_outer = a;b_inner = b;b_outer = b;% 30mm
% 
% 
% a1=zeros(row,col);
% for i=1:row
%     for j=1:col  
%         check = (i-a_inner).^2+(j-b_inner).^2-r1^2;
%         if check<=0
%             a1(i,j)=1;
% 
%         end
%         check = (i-a_outer).^2+(j-b_outer).^2-r2^2;
%         if check>=0
%             a1(i,j)=1;
%         end
%     end
% end 
% 
% % str_read = strcat(str,str_append,'\C001H001S0001','\PIV_session\???.bmp');
% 
% str_read = ('C:\Users\QCH\Desktop\冷态平均(部分)\3540_20mm.bmp');
% img1 = imread(str_read);     %读取图像
% [m,n,k]=size(img1);
% PIV_a = m-12;
% PIV_b = n-16;
% A=zeros(PIV_a,PIV_b);
% A=uint8(A);
% img=cat(3,A,A,A);
% for i=1:PIV_a
%     for j=1:PIV_b
%         img(i,j,:)=img1(i+5,j+8,:);%裁掉边框的部分
%     end
% end
% img=imresize(img,[row col]);
% for i=1:row
%     for j=1:col 
%         if a1(i,j)==1
%             img(i,j,1)=256;
%             img(i,j,2)=256;
%             img(i,j,3)=256;
%         end
%     end
% end 
% imshow(img);
% % imwrite(img,str_read);